package com.dhms.uikit

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

open class TitleBottomSheet : BottomSheetDialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return TitleBottomSheetDialog(
            requireContext(),
            R.style.Theme_UiKit_BottomSheetDialog,
            getTitle()
        )
    }

    open fun getTitle(): String? {
        return null
    }

    class TitleBottomSheetDialog(context: Context, theme: Int, title: String?) :
        BottomSheetDialog(context, theme) {
        private var _title: String? = title

        override fun setContentView(@LayoutRes layoutResId: Int) {
            super.setContentView(layoutResId)
            NotImplementedError("TitleBottomSheetDialog.setContentView(@LayoutRes layoutResId: Int) is not supported")
        }

        override fun setContentView(view: View) {
            super.setContentView(view)
            layoutInflater.inflate(
                R.layout.fragment_title_bottom_sheet_header,
                view as ViewGroup,
                true
            )
            _title?.let {
                wrapTitleView(view as ViewGroup)
            }
        }

        override fun setContentView(view: View, params: ViewGroup.LayoutParams?) {
            super.setContentView(view, params)
            _title?.let {
                wrapTitleView(view as ViewGroup)
            }
        }

        private fun wrapTitleView(parentView: ViewGroup) {
            layoutInflater.inflate(
                R.layout.fragment_title_bottom_sheet_header,
                parentView,
                true
            )
            parentView.findViewById<TextView>(R.id.title_bottom_sheet_header_title).text = _title
            parentView.findViewById<View>(R.id.title_bottom_sheet_header_close).setOnClickListener {
                dismiss()
            }
        }
    }
}
